package com.wushi.lenovo.asia5b.app_ui.credit.fragment;

import android.Manifest;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.luck.picture.lib.permissions.RxPermissions;
import com.mvp.base.BaseMvpDf;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.credit.mvpview.DownPdfDFView;
import com.wushi.lenovo.asia5b.app_di.credit.presenter.DownPdfDFPresenter;
import com.wushi.lenovo.asia5b.app_event.DownPdfEvent;
import com.wushi.lenovo.asia5b.app_model.credit.BaseHisModel;
import com.wushi.lenovo.asia5b.app_ui.other.activity.PdfPreviewActivity;
import com.wushi.lenovo.asia5b.app_ui.other.activity.PdfPreviewImageActivity;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.adapter.DownPdfAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class DownPdfDF extends BaseMvpDf<DownPdfDFView, DownPdfDFPresenter> implements DownPdfDFView {
    @BindView(R.id.iv_cancelsure_ok)
    ImageView iv_cancelsure_ok;
    @BindView(R.id.rv_downloadtrans)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_downpdf_title)
    TextView tv_downpdf_title;
    public final List<BaseHisModel> mDatas = new ArrayList<>();
    private DownPdfAdapter mAdapter;
    public String downpdf_title = "";

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_downpdf;
    }

    @Override
    protected void initView() {
        tv_downpdf_title.setText(downpdf_title);
//        if (getArguments().containsKey("data")) {
//            BaseHisModel baseHisModel = (BaseHisModel) getArguments().getSerializable("data");
//            mDatas.add((baseHisModel));
//        } else if (getArguments().containsKey("datas")) {
//            List<BaseHisModel> datas = (List<BaseHisModel>) getArguments().getSerializable("datas");
//            mDatas.addAll(datas);
//        }
        mAdapter = new DownPdfAdapter(mDatas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 查看pdf
     *
     * @param filePath 预览PDF文件路径
     * @param orderNo  预览PDF订单号
     */
    private void watchPdf(String filePath, String orderNo) {
        if (filePath.endsWith(".pdf")) {
            PdfPreviewActivity.startPdfPreviewActivity(getContext(), filePath, orderNo);
        } else {
            PdfPreviewImageActivity.startPdfPreview2Activity(getContext(), filePath, orderNo);
        }
        dismiss();
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            BaseHisModel model = mAdapter.getItem(position);
            if (view.getId() == R.id.iv_downpdf_downicon) {//下载
                new RxPermissions(mActivity)
                        .request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(aBoolean -> {
                            if (aBoolean) {
                                getPresenter().downloadPdf(mActivity, model);
                            }
                        });

            } else if (view.getId() == R.id.tv_downpdf_view) {//预览
                Object tag = view.getTag();
                if (tag != null && tag instanceof String) {
                    String filePath = (String) tag;
                    watchPdf(filePath, model.getReference_number());
                    dismiss();
                } else {
                    ToastUtil.showToast(getString(R.string.addressempty));
                }
            }
        });
    }


    @OnClick({R.id.iv_cancelsure_ok})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancelsure_ok: {
                dismiss();
            }
            break;
            default:
                break;
        }

    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getWidthStyle() {
        return getScreenWidth(0.7f);
    }

    @Override
    protected boolean isCancelableOutside() {
        return false;
    }


    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    public void downloadPdfResult(BaseHisModel model, String filePath) {
        String toast = mActivity.getString(R.string.save_success) + ":" + filePath;
        Toast.makeText(getContext(), toast, Toast.LENGTH_LONG).show();
        mAdapter.notifyDataSetChanged();
        watchPdf(filePath, model.getReference_number());
        EventBus.getDefault().post(new DownPdfEvent(model.getReference_number()));
    }
}
